package com.attendance.service;

import com.alibaba.fastjson.JSONObject;
import com.attendance.annotation.Supplement;
import com.attendance.annotation.type.OperationType;
import com.attendance.mapper.NoticeMapper;
import com.attendance.pojo.Notice;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 公告服务impl
 *
 * @author 今朝
 * @date 2021/03/21
 */
@Service
public class NoticeServiceImpl {
    @Autowired
    NoticeMapper noticeMapper;

    public List<Notice> getList() {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("idx");
        return noticeMapper.selectList(queryWrapper);
    }

    public Page<Notice> getListPage(Integer pageNum, Integer limit) {
        Page<Notice> page = new Page<>(pageNum, limit);
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("idx");
        noticeMapper.selectPage(page, queryWrapper);
        return page;
    }

    public Page<Notice> getListPage(Integer pageNum, Integer limit, String searchParams) {
        JSONObject parse = JSONObject.parseObject(searchParams);
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        for (String key : parse.keySet()) {
            queryWrapper.like(key, parse.get(key));
        }
        queryWrapper.orderByAsc("idx");
        Page<Notice> page = new Page<>(pageNum, limit);
        noticeMapper.selectPage(page, queryWrapper);
        return page;
    }

    public Notice selectById(Long id) {
        return noticeMapper.selectById(id);
    }

    @Supplement(type = OperationType.UPDATE)
    public Boolean updateById(Notice notice) {
        return noticeMapper.updateById(notice) != 0;
    }

    @Supplement(type = OperationType.PUT)
    public Boolean add(Notice notice) {
        return noticeMapper.insert(notice) != 0;
    }

    public Boolean delete(List<Integer> ids) {
        return ids.size() == noticeMapper.deleteBatchIds(ids);
    }

}
